use serde::{Deserialize, Serialize};
use utils::config::*;

/// Infra应用特定配置
#[derive(Debug, Deserialize, Clone)]
pub struct InfraConfig {
    /// 通用应用配置
    #[serde(flatten)]
    pub app: AppConfig,
    
    /// Infra特定配置
    pub infra: InfraSpecificConfig,
}

/// Infra服务特定配置
#[derive(Debug, Deserialize, Clone)]
pub struct InfraSpecificConfig {
    /// 字典服务配置
    pub dict: DictServiceConfig,
    /// 文件服务配置
    pub file: FileServiceConfig,
    /// 通知服务配置
    pub notification: NotificationServiceConfig,
    /// 黑名单服务配置
    pub blacklist: BlacklistServiceConfig,
}

/// 字典服务配置
#[derive(Debug, Deserialize, Clone)]
pub struct DictServiceConfig {
    /// 是否启用字典缓存
    pub enable_cache: bool,
    /// 缓存过期时间（秒）
    pub cache_ttl: u64,
    /// 缓存键前缀
    pub cache_prefix: String,
}

/// 文件服务配置
#[derive(Debug, Deserialize, Clone)]
pub struct FileServiceConfig {
    /// 文件存储根目录
    pub storage_path: String,
    /// 允许的文件类型
    pub allowed_types: Vec<String>,
    /// 最大文件大小（字节）
    pub max_file_size: u64,
    /// 是否启用缩略图
    pub enable_thumbnail: bool,
}

/// 通知服务配置
#[derive(Debug, Deserialize, Clone)]
pub struct NotificationServiceConfig {
    /// 邮件服务配置
    pub email: EmailConfig,
    /// 短信服务配置
    pub sms: SmsConfig,
    /// 推送服务配置
    pub push: PushConfig,
}

/// 邮件配置
#[derive(Debug, Deserialize, Clone)]
pub struct EmailConfig {
    /// SMTP服务器
    pub smtp_host: String,
    /// SMTP端口
    pub smtp_port: u16,
    /// 用户名
    pub username: String,
    /// 密码
    pub password: String,
    /// 发送者邮箱
    pub from_email: String,
    /// 发送者名称
    pub from_name: String,
}

/// 短信配置
#[derive(Debug, Deserialize, Clone)]
pub struct SmsConfig {
    /// 短信服务商
    pub provider: String,
    /// API密钥
    pub api_key: String,
    /// API密钥ID
    pub api_secret: String,
    /// 签名
    pub signature: String,
}

/// 推送配置
#[derive(Debug, Deserialize, Clone)]
pub struct PushConfig {
    /// 推送服务商
    pub provider: String,
    /// 应用ID
    pub app_id: String,
    /// 应用密钥
    pub app_key: String,
    /// 应用Secret
    pub app_secret: String,
}

/// 黑名单服务配置
#[derive(Debug, Deserialize, Clone)]
pub struct BlacklistServiceConfig {
    /// 是否启用IP黑名单
    pub enable_ip_blacklist: bool,
    /// 是否启用用户黑名单
    pub enable_user_blacklist: bool,
    /// 黑名单缓存过期时间（秒）
    pub cache_ttl: u64,
}

impl Default for InfraSpecificConfig {
    fn default() -> Self {
        Self {
            dict: DictServiceConfig::default(),
            file: FileServiceConfig::default(),
            notification: NotificationServiceConfig::default(),
            blacklist: BlacklistServiceConfig::default(),
        }
    }
}

impl Default for DictServiceConfig {
    fn default() -> Self {
        Self {
            enable_cache: true,
            cache_ttl: 3600, // 1小时
            cache_prefix: "dict:".to_string(),
        }
    }
}

impl Default for FileServiceConfig {
    fn default() -> Self {
        Self {
            storage_path: "./uploads".to_string(),
            allowed_types: vec![
                "image/jpeg".to_string(),
                "image/png".to_string(),
                "image/gif".to_string(),
                "application/pdf".to_string(),
                "text/plain".to_string(),
            ],
            max_file_size: 10 * 1024 * 1024, // 10MB
            enable_thumbnail: true,
        }
    }
}

impl Default for NotificationServiceConfig {
    fn default() -> Self {
        Self {
            email: EmailConfig::default(),
            sms: SmsConfig::default(),
            push: PushConfig::default(),
        }
    }
}

impl Default for EmailConfig {
    fn default() -> Self {
        Self {
            smtp_host: "smtp.gmail.com".to_string(),
            smtp_port: 587,
            username: "".to_string(),
            password: "".to_string(),
            from_email: "noreply@example.com".to_string(),
            from_name: "System".to_string(),
        }
    }
}

impl Default for SmsConfig {
    fn default() -> Self {
        Self {
            provider: "aliyun".to_string(),
            api_key: "".to_string(),
            api_secret: "".to_string(),
            signature: "".to_string(),
        }
    }
}

impl Default for PushConfig {
    fn default() -> Self {
        Self {
            provider: "jpush".to_string(),
            app_id: "".to_string(),
            app_key: "".to_string(),
            app_secret: "".to_string(),
        }
    }
}

impl Default for BlacklistServiceConfig {
    fn default() -> Self {
        Self {
            enable_ip_blacklist: true,
            enable_user_blacklist: true,
            cache_ttl: 1800, // 30分钟
        }
    }
}

impl Default for InfraConfig {
    fn default() -> Self {
        let mut app_config = AppConfig {
            server: ServerConfig {
                name: "infra-server".to_string(),
                port: 8081,
                api_prefix: "/api/v1/infra".to_string(),
                ..Default::default()
            },
            database: DatabaseConfig::default(),
            redis: RedisConfig::default(),
            jwt: JwtConfig::default(),
            log: LogConfig::default(),
            rpc: utils::rpc::RpcConfig::default(),
            nacos: None,
        };
        
        // 调整RPC端口为infra服务
        app_config.rpc.system.port = 50050;
        app_config.rpc.infra.dict.port = 50052;
        app_config.rpc.infra.notice.port = 50053;
        app_config.rpc.infra.file.port = 50054;
        
        Self {
            app: app_config,
            infra: InfraSpecificConfig::default(),
        }
    }
} 